<?php
/*******************************************
	Configuration file
*******************************************/

ini_set("display_errors", "0");
//echo $_SERVER["HTTP_USER_AGENT"];exit;
// Avoid direct access
if(substr_count($_SERVER['PHP_SELF'], 'init.php') > 0) {
	header("HTTP/1.0 404 Not Found");
	exit;
}

// Required Constants
#define("HTTP", 'http://soasfhtest.da.ulcc.ac.uk');
define("HTTP", 'http://digital.info.soas.ac.uk');
#define("MOBILE", HTTP.'/php/mobile');
define("MOBILE", 'http://mobile.info.soas.ac.uk');
#define("WIKI", 'http://soasfhtest.da.ulcc.ac.uk/wiki');
define("WIKI", 'http://digital.info.soas.ac.uk/wiki');
define("REPOSITORY_MAIL", "email_deposit@info.soas.ac.uk");
define("CRYPT_KEY", 'd6$ku@T90Gsg59QLjsu4');
define("LEFT_SEPARATOR", 'XX__');
define("RIGHT_SEPARATOR", '__XX');
define("ITEM_PER_PAGE", 10);
define("DEFAULT_LANGUAGE", "en_EN");
define("ANONYMOUS_USER_EMAIL", "mobile@soas.ac.uk");
define("ANONYMOUS_USER_PWD", "m0b1l3");
define("CAPTCHA_COLOR", "333333");
define("CAPTCHA_BGCOLOR", "F3EEE4");
define("CAPTCHA_DIR", substr(dirname(__FILE__), 0, strlen(dirname(__FILE__)) -3)."captcha");
define("CAPTCHA_FONT", substr(dirname(__FILE__), 0, strlen(dirname(__FILE__)) -3)."fonts/captcha.ttf");

// Requires
require_once("security.php");
require_once("curl.php");
require_once("user.php");
require_once('captcha.php');

$lang_file = substr(dirname(__FILE__), 0, strlen(dirname(__FILE__)) -3)."languages/".DEFAULT_LANGUAGE.".php";
if(is_file($lang_file)) {
	require_once($lang_file);
}

// Custom iPhone and iPad functions (to allow upload)
$isiOS = false;
if ((substr_count(strtolower($_SERVER['HTTP_USER_AGENT']), "iphone") > 0) || (substr_count(strtolower($_SERVER['HTTP_USER_AGENT']), "ipad") > 0)) {
	$isiOS = true;
}

require_once("content.php");
require_once("image.php");

require_once("pagination.php");




// Init


// CORE PAGE VARIABLES
$user = new User();

$url 				= "";
$main_menu			= "";
$breadcrumb 		= "";
$breadcrumb_items	= null;
$content			= false;
$page				= Content::get_page($_GET['page']);


if($page === false) {
	header("HTTP/1.0 404 Not Found"); exit;
} else {

	// verify page permissions
	if(Security::verify_page_permissions($page) && !$user->isLogged) {
		header("Location: login?redirect=".$page); exit;
	}
	
	// deny access to some pages if a user is already logged in
	if($user->isLogged && (($page == "login") || ($page == "register"))) {
		header("Location: home?".htmlspecialchars(SID));
	}

	// Pages
	switch($page) {
	
		case "about":
			$url = WIKI."/index.php/About?action=render";
			break;
		case "collections":
		
			// try to get collections
			if( ($collections = Content::get_collections()) === false ) {
				// in case there are not collections, I just use the standard dynamic text
				$url = WIKI."/index.php/Collection_landing?action=render";
			} else {
			
				$session = "";
				if($user->isLogged) {
					$session = "&".htmlspecialchars(SID);
				}			
			
				$url = false;
				$content = "<ul>";
				$intro = "";
				//var_dump($collections);
				foreach($collections as $collection) {
				
					// intro is the same for all the collections
					$intro = $collection["intro"];
					
					$content .= "<li><a href='collection?collection=".$collection["id"]."$session' title='".$collection["title"]."'>".$collection["title"]."</a></li>";
				
				}
				$content .= "</ul>";
				
				// attach intro
				$content = $intro.$content;
				
			}
			
			break;
			
		case "collection":

			// if no collection 
			// or collection doesn't exist
			if( (empty($_GET['collection']) ) || (($collection = Content::get_collections($_GET['collection'])) === false)) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: collections".$session);
				exit;
			}	
			
			$url = false;
			$vars = array();
			
			$session = "";
			if($user->isLogged) {
				$session = "&".htmlspecialchars(SID);
			}			
			
			$breadcrumb_items	= array(
													"Collections"			=> "collections",
													$collection["title"]	=> null
										);

			
			$vars["title"]			= $collection["title"];
		
			if(substr_count($collection["image"]["src"], "http") == 0) {
				$vars["image_src"]		= HTTP.$collection["image"]["src"];
			} else {
				$vars["image_src"]		= $collection["image"]["src"];
			}
			
			$vars["image_alt"]		= $collection["image"]["alt"];
			$vars["image_width"]	= $collection["image"]["width"];
			$vars["image_height"]	= $collection["image"]["height"];
			$vars["description"]	= $collection["description"];
			$vars["info"]			= "collection-info?collection=".urlencode($collection["id"]).$session;
			$vars["browse"]			= "browse-collection?collection=".urlencode($collection["id"]).$session;
			$backlink				= urlencode(MOBILE."/collection?collection=".$_GET['collection']."".$session);
			$vars["search"]			= "search?collection=".urlencode($collection["id"]).$session."&back=".$backlink;
			$vars["ethnic_group"]	= "";
			if(count($collection["ethnic_group"]) > 0) {
				//$vars["ethnic_group"] = Content::get_static_content(Content::CONTENT_COLLECTION_MAINTAB, $vars);
			}
			
			$vars["region"]			= "";
			
			
			$content 				= Content::get_static_content(Content::CONTENT_COLLECTION_MAINTAB, $vars);
			
			break;	
			
			
		case "collection-info":

			// if no collection 
			// or collection doesn't exist
			if( (empty($_GET['collection']) ) || (($collection_items = Content::get_collection_items($_GET['collection'])) === false)) {
				$session = "";
				if($user->isLogged) {
					$session = "&".htmlspecialchars(SID);
				}
				header("Location: collection?collection=".$_GET['collection'].$session);
				exit;
			}	
			
			$url = false;
			$vars = array();
			
			// display the requested subpage
			list($title, $link1) = each($collection_items);
			$tab = $link1;
			if(!empty($_GET["tab"])) {
				$tab	= $_GET["tab"];
				$title	= array_keys($collection_items, $tab);
			}			
			if(is_array($title)) $title = $title[0];
			
			$collection = Content::get_collections($_GET['collection']);
			
			$session = "";
			if($user->isLogged) {
				$session = "&".htmlspecialchars(SID);
			}			
			
			$vars["title"]		= "";
			$vars["submenu"]	= Content::get_collection_submenu($collection_items, $tab, $_GET['collection'], $user->isLogged);
					
			
			$vars["content"]	= Content::get_collection_info($tab);
			
			$breadcrumb_items	= array(
													"Collections"			=> "collections",
													$collection["title"]	=> "collection?collection=".$_GET['collection'],
													$title					=> null
										);
			
			
			
			$content 				= Content::get_static_content(Content::CONTENT_COLLECTION_INFO, $vars);			
			
		
			break;	
			
			
		case "browse-collection":
		
			$_GET['q']	= $_GET['collection'];

			$url		= false;
			$session	= "";
			$start		= 1;
			if(!empty($_GET["s"])) {
				$start = $_GET["s"];
			}
			
			if($user->isLogged) {
				$session = str_replace("PHPSESSID=", "", htmlspecialchars(SID));
			}
					
			$result	= Content::browse_collection($_GET["collection"], $start-1, ITEM_PER_PAGE, $_GET['order']);	
			
			if($result === false) {
				
								
			
			} else {

				$breadcrumb_items	= array(
														"Collections"			=> "collections",
														$result["title"]		=> "collection?collection=".$_GET['collection'],
														"Browse"				=> null
											);

				$images = "";
				foreach($result["items"] as $item) {

					//$img_src = Image::get_image($item["img"], "100", true); THE IMAGE IS SMALL ENOUGH, we save some time without tinysrc
					$img_src = $item["img"];
					
					// get link
					$link = "view?image={$item['id']}&tab=main&s=".$start."&order=".$_GET['order'];
					$back_link = "fullscreen?image=".$item['id']."&back=".urlencode("browse-collection?collection=".$_GET["collection"]."&s=".$start."&order=".$_GET['order']);
					if($user->isLogged) {
						$link = "view?".htmlspecialchars(SID)."&image={$item['id']}&tab=main&s=".$start."&order=".$_GET['order'];
						$back_link = "fullscreen?image=".$item['id']."&back=".urlencode("browse-collection?collection=".$_GET["collection"]."&s=".$start."&order=".$_GET['order']."&".htmlspecialchars(SID));
					}							
								
									
					$vars = array(
									"imgsrc"				=> $img_src,
									"caption"				=> "<a href='$link' title='{$item['title']}'>".$item['title']."</a>",
									"fullscreen"			=> $back_link
					);
					$images	.= Content::get_static_content(Content::CONTENT_IMAGE_CAPTIONONLY, $vars);	

				}								
				
				
				
				// hidden fields
				$hidden_fields = "";
				$fields = explode("&", $_SERVER['REQUEST_URI']);
				for($i=0; $i<count($fields);$i++) {
					$field = explode("=", $fields[$i]);
					
					if(substr_count($field[$i], "?") > 0) {
						$tmp = explode("?", $field[0]);
						$field[0] = $tmp[1];
					}
					
					if( ($field[0] != "order") && ($field[0] != "sort")) {
						$hidden_fields .= "<input type='hidden' name='{$field[0]}' id='{$field[0]}' value='".urldecode($field[1])."' />";
					}
				}
				
				// order
				$datesel		= "";
				$datedescsel	= "";
				$titlesel		= "";
				$titledescsel	= "";
				
				if(strpos($_GET['order'], 'date') === 0) {
					$datesel		= "selected='selected'";
				} else if(strpos($_GET['order'], '-date') === 0) {
					$datedescsel	= "selected='selected'";
				} else if(strpos($_GET['order'], 'title') === 0) {
					$titlesel		= "selected='selected'";
				} else if(strpos($_GET['order'], '-title') === 0) {
					$titledescsel	= "selected='selected'";
				} else {
					$titlesel		= "selected='selected'";
				}			
				
				
				$vars = array(
								"images"				=> $images,
								"action"				=> "browse-collection",
								"hidden_fields"			=> $hidden_fields,
								"datesel"				=> $datesel,
								"datedescsel"			=> $datedescsel,
								"titledescsel"			=> $titledescsel,
								"titlesel"				=> $titlesel,
								"pagination"			=> Content::get_pagination(str_replace("&s=".$_GET['s'], "", $_SERVER['REQUEST_URI']), $start, $result["total"], ITEM_PER_PAGE)
				);
				$content	= Content::get_static_content(Content::CONTENT_BROWSE_COLLECTION, $vars);	
				
								

			} 

		
			break;	
			
		case "view":
		
			// if no images 
			if(empty($_GET['image']) ) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: collections".$session);
				exit;
			}
			
			$url				= false;
			
			$image = Content::get_image($_GET['image']);
			// if image doesn't exist
			if($image === false) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: collections".$session);
				exit;
			}			
			
			$start		= 1;
			if(!empty($_GET["s"])) {
				$start = $_GET["s"];
			}
			
			$breadcrumb_items	= array(
													"Collections"					=> "collections",
													$image["collection"]["title"]	=> "collection?collection=".urlencode($image["collection"]["id"]),
													"Browse"						=> "browse-collection?collection=".urlencode($image["collection"]["id"])."&s=".$start."&order=".$_GET['order']
												);	
			
			// in case the previus page is the serche page
			$referer = "";
			if(substr_count($_SERVER["HTTP_REFERER"], "search-result") > 0) {
				$referer = '<p class="top"><a title="Go back to search results" href="'.$_SERVER["HTTP_REFERER"].'">Back to results</a></p>';
			}
			
			// display the requested subpage
			$tab				= $_GET["tab"];
			switch($tab) {
			
				case "main":default:	// main page				
				
					$breadcrumb_items["Image"] = null;			
				
					//$image["imgsrc"]	= Image::get_image($image["img"], "100", true);
					$image["imgsrc"]	= "get-image?image_src=".urlencode($image["img"])."&size=100&data=true";
					$image["submenu"]	= Content::get_image_submenu($_GET['image'], "main", $user->isLogged);
					
					$back_link = "fullscreen?image=".$_GET['image']."&back=".urlencode("view?image=".$_GET['image']."&tab=main".$_GET["q"]."&s=".$start);
					$save_in_searches = "";
					if($user->isLogged) {
						$back_link = "fullscreen?image=".$_GET['image']."&back=".urlencode("view?image=".$_GET['image']."&tab=main".$_GET["q"]."&s=".$start."&".htmlspecialchars(SID));
						$tmp = array(
										'session'	=> htmlspecialchars(SID),
										'eprintid'	=> (int)$_GET['image']
						);
						$save_in_searches = Content::get_static_content(Content::CONTENT_SAVE_IMAGE, $tmp);
					}
					
					$image["fullscreen"]	= $back_link;
					
					// short description, if needed
					$session = "";
					if($user->isLogged) {
						$session = "&".htmlspecialchars(SID);
					}
					if( (strlen($image["description"]) > 300) && ((int)$_GET['read_more'] != 1) ) {
						$image["description"] = substr($image["description"], 0, 300)."... [<a title='".Content::MENU_READ_MORE."' href='view?image=".$_GET['image']."&tab=main&read_more=1$session'>".Content::MENU_READ_MORE."</a>]";
					} else if((int)$_GET['read_more'] == 1) {
						$image["description"] = $image["description"]." [<a title='".Content::MENU_READ_LESS."' href='view?image=".$_GET['image']."&tab=main$session'>".Content::MENU_READ_LESS."</a>]";
					}
					
					if(!array_key_exists("date", $image["date"])) {
						$image["date"] = "-";
					}
					
					
					$image["referer"]	= $referer;
					$image["save_in_searches"] = $save_in_searches;
					$content			= Content::get_static_content(Content::CONTENT_IMAGE_MAINTAB, $image);
					break;
					
				case "info":

									
					$breadcrumb_items["Info"] = null;			


					$image["submenu"]	= Content::get_image_submenu($_GET['image'], "info", $user->isLogged);
					
					$image["collection"] = $image["collection"]["title"];
					
					$image["items"]		= "";
					
					foreach($image["extra"] as $misc) {
					
						$image["items"]		.= Content::get_static_content(Content::CONTENT_IMAGE_INFOTAB_ITEM, $misc);
					
					}
					
					$content			= Content::get_static_content(Content::CONTENT_IMAGE_INFOTAB, $image);
					break;
					
				case "comments":			
				
					$start		= 1;
					if(!empty($_GET["s"])) {
						$start = $_GET["s"];
					}				
					
					$total  = 0;
				
					// if no comments to the image
					if(($items = Content::get_image_comments($_GET['image'], $start-1, ITEM_PER_PAGE)) === false) {
						$comments["COMMENTS"] = Content::COMMENTS_NOCOMMENT;
					} else {
					
						$session = "";
						if($user->isLogged) {
							$session = "&".htmlspecialchars(SID);
						} 
					
						$comments["COMMENTS"] = "";
						
						foreach($items as $item) {
						
							$user_content = "";
							
							if($user->user_id == $item["user_id"]) {
								$user_content = "<a href='view?image=".$_GET['image']."&tab=comments&s=$start&comment=".$item["comment_id"]."".$session."#comment-".$item["comment_id"]."' title='Edit'>Edit</a> | <a href='view?image=".$_GET['image']."&tab=delete-comment&s=$start&comment=".$item["comment_id"]."".$session."' title='Delete'>Delete</a>";
								
								if($_GET["comment"] == $item["comment_id"]) {
								
									$vars = array(
										"action"	=> "view?image=".$_GET['image']."&tab=update-comment&comment=".$item["comment_id"]."".$session,
										"comment"		=> $item["text"],
										"imageid"	=> (int)$_GET["image"],
										"commentid"	=> (int)$_GET["comment"],
										"target"	=> "/".(int)$_GET["image"]."/",
										"s"			=> $start,
										"cancel"	=> "view?image=".$_GET['image']."&tab=comments&s=$start".$session."#comment-".$item["comment_id"]
									);
									
									$item["text"] = "";
								
									$user_content = Content::get_static_content(Content::COMMENTS_EDITCOMMENTFORM, $vars);
									
									
								}
								
							}
						
							$vars = array (
											"comment_id"	=> "comment-".$item["comment_id"],
											"title" 		=> $item["user_name"]." wrote on ".date('d F Y (H:i)', strtotime($item["creation_date"])),
											"comment"		=> $item["text"],
											"user"			=> $user_content
							);
							
							$total = $item["total"];
						
							$comments["COMMENTS"]	.= Content::get_static_content(Content::COMMENTS_COMMENT, $vars);								
							
						}						
						
						
					}			
					
				
					// Add comments
					$captcha = "";
					$not_logged = "";
					$session = htmlspecialchars(SID);
					if(!$user->isLogged) {
						// captcha code
						$key = Captcha::createCode(8);
						$word = Captcha::createCode(4);
						$encoded = Captcha::Crypt($word, $key); 
						$session = "session=".$encoded.$key;
						
						Captcha::genCaptcha($word, $encoded.$key);
						
						$captcha = "<img src='".MOBILE."/resources/captcha/".$encoded.$key.".png' />".Content::COMMENT_CAPTCHA;
						$not_logged = Content::COMMENT_NOTLOGGED;
					} 	
					
					$vars = array(
						"comment"				=> "",
						"imageid"				=> (int)$_GET['image'],
						"action"				=> "view?image=".$_GET['image']."&tab=add-comment&".$session,
						"recaptcha"				=> $captcha,
						"not_logged"			=> $not_logged,
						"error"					=> $error
					);
					$comments["add_comment"]	= Content::get_static_content(Content::COMMENTS_COMMENTFORM, $vars);
															
					
				
					$breadcrumb_items["Comments"] = null;		
												
					$comments["submenu"]	= Content::get_image_submenu($_GET['image'], "comments", $user->isLogged);

					$comments["title"]		= $image["title"];		
					
					// Thumb image on top
					$comments["image_src"]	= $image["thumb"];	
					
					$comments["pagination"]	= Content::get_pagination(str_replace("&s=".$_GET['s'], "", $_SERVER['REQUEST_URI']), $start, $total, ITEM_PER_PAGE);			
				
					$content				= Content::get_static_content(Content::CONTENT_IMAGE_COMMENTTAB, $comments);
				
					break;
					
				case "add-comment":
	
									
					$error = "";
					if(isset($_POST["eprintid"])) {
					
						$code = "";
						if(!$user->isLogged) {
							$encoded = $_GET['session'];
							
							$key = substr($encoded, strlen($encoded)-8, 8);
							$word = substr($encoded, 0, strlen($encoded)-8);
							$code = Captcha::deCrypt($word,$key);
						}
					
						$result = Content::add_image_comment($_GET['image'], $user, $_POST, "text", $code);
						
						if($result === false) {
							$error = "<p class='error'>".$user->error."</p>";
						} else {
						
							$session = "";
							if($user->isLogged) {
								$session = "&".htmlspecialchars(SID);
							}

							header("Location: view?image=".$_GET['image']."&tab=comments".$session."#comments");
							exit;
						}
					}
				
				
					$captcha = "";
					$session = htmlspecialchars(SID);
					$not_logged = "";
					
					if(!$user->isLogged) {
						// captcha code
						$key = Captcha::createCode(8);
						$word = Captcha::createCode(4);
						$encoded = Captcha::Crypt($word, $key); 
						$session = "session=".$encoded.$key;
						Captcha::genCaptcha($word, $encoded.$key);
						
						$captcha = "<img src='".MOBILE."/resources/captcha/".$encoded.$key.".png' />".Content::COMMENT_CAPTCHA;
						$not_logged = Content::COMMENT_NOTLOGGED;
					}
											
					$vars = array(
						"comment"				=> $_POST['text'],
						"imageid"				=> (int)$_GET['image'],
						"action"				=> "view?image=".$_GET['image']."&tab=add-comment&".$session,
						"recaptcha"				=> $captcha,
						"not_logged"			=> $not_logged,
						"error"					=> $error
					);
					$content["content"]			= Content::get_static_content(Content::COMMENTS_COMMENTFORM, $vars);
				
				
				
					$content["submenu"]	= Content::get_image_submenu($_GET['image'], "add-comment", $user->isLogged);							
				
					$content["title"]	= $image["title"];							
					
					$breadcrumb_items["Add"] = null;		
												
													
				
					$content				= Content::get_static_content(Content::CONTENT_IMAGE_ADDCOMMENTTAB, $content);
				
					break;
					
					
				case "update-comment":
					// if the comment is not set or the user is not logged in
					if( ((int)$_GET["comment"] == 0) || (!$user->isLogged) ) {
						$session = "";
						if($user->isLogged) {
							$session = "?".htmlspecialchars(SID);
						}
						header("Location: collections".$session);
						exit;
					}
					
					$start		= 1;
					if(!empty($_POST["s"])) {
						$start = $_POST["s"];
					}						

					// Update comment and reload comments
					$post_data = array(
						"text"		=> ($_POST["text"]),
						"eprintid"	=> (int)$_GET["image"],
						"commentid"	=> (int)$_GET["comment"],
						"target"	=> "/".(int)$_GET["image"]."/"
					);
					
					Content::update_image_comment($user, $post_data);
					
					header("Location: view?image=".$_GET['image']."&tab=comments&s=$start&".htmlspecialchars(SID)."#comment-".(int)$_GET["comment"]);
					exit;					
					
					break;

				case "delete-comment":
					// if the comment is not set or the user is not logged in
					if( ((int)$_GET["comment"] == 0) || (!$user->isLogged) ) {
						$session = "";
						if($user->isLogged) {
							$session = "?".htmlspecialchars(SID);
						}
						header("Location: collections".$session);
						exit;
					}
					
					$start		= 1;
					if(!empty($_GET["s"])) {
						$start = $_GET["s"];
					}						
					
					// Delete comment and reload comments
					$post_data = array(
						"text"		=> "",
						"eprintid"	=> (int)$_GET["image"],
						"commentid"	=> (int)$_GET["comment"],
						"target"	=> "/".(int)$_GET["image"]."/",
						"delete"	=> "Delete"
					);
					
					Content::delete_image_comment($user, $post_data);
					
					header("Location: view?image=".$_GET['image']."&tab=comments&s=$start&".htmlspecialchars(SID));
					exit;
					
					break;
					
					
				case "related-images":			
				
					$start		= 1;
					if(!empty($_GET["s"])) {
						$start = $_GET["s"];
					}				
					
					$total  = 0;
					$hidden_fields = "";
					$datesel		= "";
					$datedescsel	= "";
					$titlesel		= "";
					$titledescsel	= "";
					
				
					// if no related images
					if(($items = Content::get_image_related($_GET['image'], $start-1, ITEM_PER_PAGE, $_GET['order'])) === false) {
						$relatedImages["RELATED"] = Content::RELATED_NORELATED;
					} else {
					
						$session = "";
						if($user->isLogged) {
							$session = "&".htmlspecialchars(SID);
						} 
					
						$relatedImages["RELATED"] = "";
						
						foreach($items as $item) {
						
							$img_src = $item["thumb"];
							
							// get link
							$link = "view?image={$_GET['image']}&tab=related-image&related-image={$item['id']}&s=".$start."&order=".$_GET['order'];
							$back_link = "fullscreen?image=".$item['id']."&back=".urlencode("view?image=".$_GET['image']."&tab=related-images&s=".$start."&order=".$_GET['order']);
							if($user->isLogged) {
								$link = "view?".htmlspecialchars(SID)."&image={$_GET['image']}&tab=related-image&related-image={$item['id']}&s=".$start."&order=".$_GET['order'];
								$back_link = "fullscreen?image=".$item['id']."&back=".urlencode("view?image=".$_GET['image']."&tab=related-images&s=".$start."&order=".$_GET['order']."&".htmlspecialchars(SID));
							}							
								
												
							$vars = array(
											"imgsrc"				=> $img_src,
											"caption"				=> "<a href='$link' title='{$item['title']}'>".$item['title']."</a>",
											"fullscreen"			=> $back_link
							);
							$relatedImages["RELATED"]	.= Content::get_static_content(Content::CONTENT_IMAGE_CAPTIONONLY, $vars);	
							
							$total = $item["total"];							
							
						}
						
					}			
					
				
					// upload related images
					$link = "view?image={$_GET['image']}&tab=related-images&s=".$start."&order=".$_GET['order'];
					if($user->isLogged) {
						$link = "view?".htmlspecialchars(SID)."&image={$_GET['image']}&tab=related-images&s=".$start."&order=".$_GET['order'];
					}
					$session = htmlspecialchars(SID);
					$uploadRelatedImages = array();
					$upload_related = Content::get_static_content(Content::RELATED_NEEDLOGIN, $uploadRelatedImages);
					if($user->isLogged) {
							$vars = array(
							"email"					=> REPOSITORY_MAIL,
							'uri' 					=> '?image='.$_GET['image']."&".$session.'&back='.urlencode($link)
						);
						$upload_related				= Content::get_static_content(Content::RELATED_SEND_FORM, $vars);
					} 	
					
					$relatedImages["send_related"]	= $upload_related;
					
					if($_GET["sent"] == 1) {
						$back = "view?image=".$_GET['image']."&tab=related-images&".htmlspecialchars(SID);
						$relatedImages["send_related"]	= '<p class="success">'.Content::RELATED_SUCCESS.' <a href="'.$back.'" title="'.Content::RELATED_GO_BACK_TITLE.'">'.Content::RELATED_GO_BACK.'</a></p>'.Content::get_static_content(Content::RELATED_SUBMISSION_ISSUES, array('uri' => '?image='.$_GET['image']."&".$session.'&back='.urlencode($link)) );


					}

					$breadcrumb_items["User-contributed items"] = null;
												
					$relatedImages["submenu"]	= Content::get_image_submenu($_GET['image'], "related-images", $user->isLogged);

					$relatedImages["title"]		= $image["title"];	
							
					
					// Thumb image on top
					$relatedImages["image_src"]	= $image["thumb"];	
					
					$relatedImages["pagination"]	= Content::get_pagination(str_replace("&s=".$_GET['s'], "", $_SERVER['REQUEST_URI']), $start, $total, ITEM_PER_PAGE);	
					
					// hidden fields
					$fields = explode("&", $_SERVER['REQUEST_URI']);
					for($i=0; $i<count($fields);$i++) {
						$field = explode("=", $fields[$i]);
						
						if(substr_count($field[$i], "?") > 0) {
							$tmp = explode("?", $field[0]);
							$field[0] = $tmp[1];
						}
						
						if( ($field[0] != "order") && ($field[0] != "sort")) {
							$hidden_fields .= "<input type='hidden' name='{$field[0]}' id='{$field[0]}' value='".urldecode($field[1])."' />";
						}
					}
			
					// order
					if(strpos($_GET['order'], 'date') === 0) {
						$datesel		= "selected='selected'";
					} else if(strpos($_GET['order'], '-date') === 0) {
						$datedescsel	= "selected='selected'";
					} else if(strpos($_GET['order'], 'title') === 0) {
						$titlesel		= "selected='selected'";
					} else if(strpos($_GET['order'], '-title') === 0) {
						$titledescsel	= "selected='selected'";
					} else {
						$titlesel		= "selected='selected'";
					}						
					
					$relatedImages["action"]		= "view";
					$relatedImages["hidden_fields"]	= $hidden_fields;
					$relatedImages["datesel"]		= $datesel;
					$relatedImages["datedescsel"]	= $datedescsel;
					$relatedImages["titledescsel"]	= $titledescsel;
					$relatedImages["titlesel"]		= $titlesel;	
				
					$content						= Content::get_static_content(Content::CONTENT_IMAGE_RELATEDTAB, $relatedImages);
				
					break;	
					
				case "related-image":

					$breadcrumb_items["User-contributed item"] = null;			

					// get related image
					$relatedImage = Content::search_by_id($_GET['related-image'], true);
					
					if($relatedImage === false) {
						$session = "";
						if($user->isLogged) {
							$session = "?".htmlspecialchars(SID);
						}
						header("Location: collections".$session);
						exit;
					}	
				
					//$image["imgsrc"]	= Image::get_image($image["img"], "100", true);
					$image["imgsrc"]	= "get-image?image_src=".urlencode($relatedImage["img"])."&size=100&data=true";
					$image["submenu"]	= Content::get_image_submenu($_GET['image'], "related-image", $user->isLogged);
					
					$back_link = "fullscreen?image=".$_GET['related-image']."&back=".urlencode("view?image=".$_GET['image']."&tab=related-image&related-image=".$_GET['related-image']."&s=".$start."&order=".$_GET['order']);
					$save_in_searches = "";
					if($user->isLogged) {
						$back_link = "fullscreen?image=".$_GET['related-image']."&back=".urlencode("view?image=".$_GET['image']."&tab=related-image&related-image=".$_GET['related-image']."&s=".$start."&order=".$_GET['order']."&".htmlspecialchars(SID));
					}
					
					$image["fullscreen"]	= $back_link;
					
					// short description, if needed
					$session = "";
					if($user->isLogged) {
						$session = "&".htmlspecialchars(SID);
					}
					
					$image["title"] = $relatedImage["title"];
					$image["description"] = $relatedImage["description"];
					$image["date"] = $relatedImage["date"];
					
					
					$image["referer"]	= $referer;
					$image["save_in_searches"] = $save_in_searches;
					$content			= Content::get_static_content(Content::CONTENT_IMAGE_MAINTAB, $image);
									
					break;		
			
			}
		
			break;
		
		case "submit-related-item":
		
			// if no images 
			if(empty($_GET['image']) ) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: ".$_GET["back"]);
				exit;
			}
			
			$url				= false;
					
		
			$vars = array(
							"image"	=> (int)$_GET['image'],
							"email"	=> REPOSITORY_MAIL,
							"back"	=> urldecode($_GET["back"])
			);
		
			$content = Content::get_static_content(Content::CONTENT_SUBMISSION_EMAIL, $vars);
		
			break;
				
			
		case "fullscreen":
		
			// if no images 
			if(empty($_GET['image']) ) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: ".$_GET["back"]);
				exit;
			}
			
			$url				= false;
			
			$image = Content::get_image($_GET['image']);
			// if image doesn't exist
			if($image === false) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: ".$_GET["back"]);
				exit;
			}			
		
			$vars = array(
							//"image"	=> Image::get_image($image["img"], "100", true),
							"image"	=> "get-image?image_src=".urlencode($image["img"])."&size=100&data=true",
							"title"	=> $image["title"],
							"back"	=> $_GET["back"]
			);
		
			echo '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>		<title>'.Content::TEMPLATE_META_TITLE.'</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link href="'.MOBILE.'/resources/styles/styles.css" rel="stylesheet" type="text/css" /></head><body>'.Content::get_static_content(Content::CONTENT_IMAGE_FULLSCREEN, $vars)."</body></html>";
		
			// we just want to show the image, nothing else
			exit;
		
			break;	
			
		case "get-image":
			if( ($_GET['image_src'] != "") && ($_GET['size'] != "") && ($_GET['data'] != "") ) {
	
				header('Content-Type: image/png');				
				echo (Image::get_image(urldecode($_GET['image_src']), $_GET['size'], true));
				
				exit;
			}
			break;				
			
			
		case "contact":
			$url = WIKI."/index.php/Contact?action=render";
			break;
		case "conditions-of-use":
			$url = WIKI."/index.php/Conditions_of_Use?action=render";
			break;			
		case "home":
			$url = WIKI."/index.php/Index?action=render";
			break;
						
		case "search":
			$url		= false;
			$session	= "";
			if($user->isLogged) {
				$session = str_replace("PHPSESSID=", "", htmlspecialchars(SID));
			}
			
			$collection = array();
			$search_in = "";
			if(!empty($_GET["collection"])) {
				$collection = Content::get_collections($_GET['collection']);
				$search_in = "in ".$collection["title"];
			}
			
			$back = "";
			if($_GET['back'] != "") {
				$back = Content::get_static_content(Content::CONTENT_SEARCH_BACK, array('back' => urldecode($_GET['back'])));
			}
			
			
			$vars = array(
							"search"				=> "",
							"back"					=> $back,
							"searchin"				=> $search_in,																			
							"collection"			=> $_GET["collection"],
							"error"					=> "",
							"session"				=> $session
			);
			$content	= Content::get_static_content(Content::CONTENT_SEARCH, $vars);			

			break;			
		case "search-result":
		
			// if there is nothing to search -> go back to the search page
			if(empty($_GET['q'])) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: search".$session);
				exit;
			}
			
			$url		= false;
			$session	= "";
			$start		= 1;
			if(!empty($_GET["s"])) {
				$start = $_GET["s"];
			}
			
			if($user->isLogged) {
				//$session = str_replace("PHPSESSID=", "", htmlspecialchars(SID));
			}
			
			$collection = array();
			$search_in = "";
			if(!empty($_GET["collection"])) {
				$collection = Content::get_collections($_GET['collection']);
				$search_in = " in ".$collection["title"];
			}			
					
			$result	= Content::simple_search($_GET['q'], $_GET['collection'], $start-1, ITEM_PER_PAGE, false, $_GET['order']);		
			
			if( ($result === false) || ((int)$result['total'] == 0) ) {
				
				$vars = array(
								"search"				=> urldecode($_GET['q']),
								"searchin"				=> $search_in,
								"collection"			=> urldecode($_GET['collection']),
								"error"					=> "<p class='error'>No results for \"".urldecode($_GET['q'])."\"$search_in</p><br />",
								"session"				=> $session
				);
				$content	= Content::get_static_content(Content::CONTENT_SEARCH, $vars);					
			
			} else {

				$images = "";
				foreach($result["items"] as $item) {

					//$img_src = Image::get_image($item["img"], "100", true); THE IMAGE IS SMALL ENOUGH, we save some time without tinysrc
					$img_src = $item["img"];
					
					// get link
					$link = "view?image={$item['id']}&tab=main";
					$back_link = "fullscreen?image=".$item['id']."&back=".urlencode("search-result?q=".$_GET["q"]."&s=".$start."&order=".$_GET['order']);
					if($user->isLogged) {
						$link = "view?".htmlspecialchars(SID)."&image={$item['id']}&tab=main";
						$back_link = "fullscreen?image=".$item['id']."&back=".urlencode("search-result?q=".$_GET["q"]."&s=".$start."&order=".$_GET['order']."&".htmlspecialchars(SID));
					}
										
									
					$vars = array(
									"imgsrc"				=> $img_src,
									"caption"				=> "<a href='$link' title='{$item['title']}'>".$item['title']."</a>",
									"fullscreen"			=> $back_link
					);
					$images	.= Content::get_static_content(Content::CONTENT_IMAGE_CAPTIONONLY, $vars);	

				}				
				
				// hidden fields
				$hidden_fields = "";
				$hidden_fields_search = "";
				$fields = explode("&", $_SERVER['REQUEST_URI']);
				for($i=0; $i<count($fields);$i++) {
					$field = explode("=", $fields[$i]);
					
					if(substr_count($field[$i], "?") > 0) {
						$tmp = explode("?", $field[0]);
						$field[0] = $tmp[1];
					}
					
					if( ($field[0] != "order") && ($field[0] != "sort")) {
						$hidden_fields .= "<input type='hidden' name='{$field[0]}' id='{$field[0]}' value='".urldecode($field[1])."' />";
					}
					
					if( ($field[0] == "collection") || ($field[0] == "PHPSESSID")) {
						$hidden_fields_search .= "<input type='hidden' name='{$field[0]}' id='{$field[0]}' value='".urldecode($field[1])."' />";
					}
				}
				
				// order
				$datesel		= "";
				$datedescsel	= "";
				$titlesel		= "";
				$titledescsel	= "";
				
				if(strpos($_GET['order'], 'date') === 0) {
					$datesel		= "selected='selected'";
				} else if(strpos($_GET['order'], '-date') === 0) {
					$datedescsel	= "selected='selected'";
				} else if(strpos($_GET['order'], 'title') === 0) {
					$titlesel		= "selected='selected'";
				} else if(strpos($_GET['order'], '-title') === 0) {
					$titledescsel	= "selected='selected'";
				} else {
					$titlesel		= "selected='selected'";
				}
				
				// Save search
				$save_search	= ""; 
				if($user->isLogged) {
					
					$search_vars	= array(
												"href"	=> 'saved-searches?'.htmlspecialchars(SID).'&q='.$_GET['q'].'&collection='.$_GET['collection'].'&order='.$_GET['order']
					);
					
					$save_search	= Content::get_static_content(Content::CONTENT_SEARCH_SAVE_BUTTON, $search_vars);	
				}
				
				$vars = array(
								"search"				=> urldecode($_GET['q']),
								"action"				=> "search-result",
								"hidden_fields"			=> $hidden_fields,
								"hidden_fields_search"	=> $hidden_fields_search,	
								"save"					=> $save_search,							
								"datesel"				=> $datesel,
								"datedescsel"			=> $datedescsel,
								"titledescsel"			=> $titledescsel,
								"titlesel"				=> $titlesel,		
								"images"				=> $images,
								"pagination"			=> Content::get_pagination(str_replace("&s=".$_GET['s'], "", $_SERVER['REQUEST_URI']), $start, $result["total"], ITEM_PER_PAGE)
				);
				$content	= Content::get_static_content(Content::CONTENT_SEARCH_RESULT, $vars);	
				
								

			} 
			
			
			
			break;	
			
		case 'saved-searches':
		
			$url		= false;
			$session	= str_replace("PHPSESSID=", "", htmlspecialchars(SID));
			$content	= "";
			$error		= "";
			$searches	= array();
			
			// Save search (if required)
			if($_GET['q'] != '') {
				$collection = false;
				if($_GET['collection'] != "") {
					$collection = false;
				}
				if($user->save_search(Content::get_search_url($_GET['q'], $_GET['collection'], $collection, $_GET['order'])) === true) {
					
					$error = '<p class="success">'.Content::SEARCH_SAVED.'</p>';
					
				} else {
					
					$error = "<p class='error'>".Content::ERROR_SAVE_SEARCH."</p><br />";
					
				}
			}
			// Save search by EPRINTID (if required)
			if($_GET['eprintid'] != '') {
				if($user->save_search(HTTP."/cgi/search/advanced?screen=Public%3A%3AEPrintSearch&_action_search=Search&eprintid=".(int)$_GET['eprintid']."&satisfyall=ALL&order=title%2F-date_of_original_from") === true) {
					
					$error = '<p class="success">'.Content::SEARCH_SAVED.'</p>';
					
				} else {
					
					$error = "<p class='error'>".Content::ERROR_SAVE_SEARCH."</p><br />";
					
				}
			}
			
			
			// Delete Search (if required)
			if( ($_GET['action'] == 'remove') && ((int)$_GET['id'] != 0) ) {
				
				if($user->remove_search((int)$_GET['id']) === true) {
					
					$error = '<p class="success">'.Content::SEARCH_DELETED.'</p>';
					
				} else {
					
					$error = "<p class='error'>".Content::ERROR_SAVE_SEARCH."</p><br />";
					
				}
			}
			// Update search (just message)
			if( ($_GET['action'] == 'updated') ) {

				$error = '<p class="success">'.Content::SEARCH_UPDATED.'</p>';
					
			}
			
			// if no saved searches
			if( ($items = $user->get_saved_searches() ) === false) {
				$searches["searches"] = Content::SEARCHES_NO_SAVED_SEARCHES;
			} else {
			
				$searches["searches"] = "";
				foreach($items as $item) {

					// switch between searches and single images
					$do_search_link = "";
					if($item["q"] != "") {
						$do_search_link = "search-result?".htmlspecialchars(SID)."&collection=".$item['collection']."&q=".urlencode($item["q"])."&search=Search&order=".urlencode($item["order"])."&sort=Sort+Result";
					} else if($item["collection"] != "") {
						$do_search_link = "browse-collection?collection=".$item['collection']."&".htmlspecialchars(SID)."&order=".urlencode($item["order"]);
					} else {
						$do_search_link = "view?".htmlspecialchars(SID)."&image=".$item['eprintid']."&tab=main";
					}

					$vars = array (
									"session"		=> htmlspecialchars(SID),
									"userid"		=> $item["user"],
									"savedsearchid"	=> $item["id"],
									"title" 		=> $item["title"],
									"do_search_link"=> $do_search_link
					);
				
					$searches["searches"]	.= Content::get_static_content(Content::SEARCHES_SEARCH, $vars);								
			
				}
			}
			
			// Show saved searches
			$searches['error']	= $error;			
			$content	= Content::get_static_content(Content::CONTENT_SEARCH_SAVED_SEARCHES, $searches);
		
			break;		
			
	case 'edit-saved-search':
		
			$url		= false;
			$session	= str_replace("PHPSESSID=", "", htmlspecialchars(SID));
			$content	= "";
			$error		= "";
			
			// if no saved search
			if( ($item = $user->get_saved_search((int)$_GET['id']) ) === false) {
				$session = "";
				if($user->isLogged) {
					$session = "?".htmlspecialchars(SID);
				}
				header("Location: saved-searches".$session);
				exit;
			}
			
			// Update saved search
			if($_POST['_action_save'] != "") {
			
				if($user->update_saved_search($_POST) === false) {
					$error = "<p class='error'>".Content::ERROR_SAVE_SEARCH."</p><br />";
				} else {
					$session = "";
					if($user->isLogged) {
						$session = "?".htmlspecialchars(SID);
					}
					header("Location: saved-searches".$session."&action=updated");
					exit;
				}
			
			}
			
			$frequency_never_sel = '';
			$frequency_daily_sel = '';
			$frequency_weekly_sel = '';
			$frequency_monthly_sel = '';
			switch($item['frequency']) {
				case 'never':
					$frequency_never_sel = 'selected="selected"';
					break;
				case 'daily':
					$frequency_daily_sel = 'selected="selected"';
					break;
				case 'weekly':
					$frequency_weekly_sel = 'selected="selected"';
					break;
				case 'monthly':
					$frequency_monthly_sel = 'selected="selected"';
					break;												
			}
			
			$mail_empty		= '<input checked="checked" value="TRUE" name="c3_mailempty" type="radio">';
			$mail_notempty	= '<input value="FALSE" name="c3_mailempty" type="radio">';
			if($item['mail'] == 'FALSE') {
				$mail_empty		= '<input value="TRUE" name="c3_mailempty" type="radio">';
				$mail_notempty	= '<input checked="checked" value="FALSE" name="c3_mailempty" type="radio">';
			}

			$search_public		= '<input checked="checked" value="TRUE" name="c4_public" type="radio">';
			$search_notpublic	= '<input value="FALSE" name="c4_public" type="radio">';
			if($item['public'] == 'FALSE') {
				$search_public		= '<input value="TRUE" name="c4_public" type="radio">';
				$search_notpublic	= '<input checked="checked" value="FALSE" name="c4_public" type="radio">';
			}			
			
			// Show saved searches
			$vars		= array(
									'error'						=> $error,
									'session'					=> "?".htmlspecialchars(SID),
									'id'						=> (int)$_GET['id'],
									'hiddenfields'				=> '<input value="'.(int)$_GET['id'].'" name="savedsearchid" type="hidden" id="savedsearchid"><input value="'.$user->user_id.'" name="userid" type="hidden" id="userid"><input value="User::SavedSearch::Edit" name="screen" type="hidden" id="screen">',
									'title'						=> "<input type='text' name='c2_name' value='".$item["title"]."' >",
									'frequency_never_sel'		=> $frequency_never_sel,
									'frequency_daily_sel'		=> $frequency_daily_sel,
									'frequency_weekly_sel'		=> $frequency_weekly_sel,
									'frequency_monthly_sel'		=> $frequency_monthly_sel,
									'mail_empty'				=> $mail_empty,
									'mail_notempty'				=> $mail_notempty,
									'search_public'				=> $search_public,
									'search_notpublic'			=> $search_notpublic
									
									
			);
			$content	= Content::get_static_content(Content::CONTENT_EDIT_SAVED_SEARCH, $vars);
		
			break;					
			
		case "login":

			$error = "";
			if(isset($_POST["login"])) {
				if($user->login($_POST['username'], $_POST['password'])) {
											
					// redirect to the last surfed page
					$session_id = $user->session_id;
					
					if(isset($_GET['redirect'])) {
						if( (substr_count($_GET["redirect"], "login") == 0) && (substr_count($_GET["redirect"], "activate-user-account") == 0) ) {
							$separator = "?";
							if(substr_count($_GET["redirect"], "?") != 0) {
								$separator = "&";
							}
							header("Location: ".urldecode($_GET["redirect"])."{$separator}PHPSESSID=".$session_id);
						} else {
							header("Location: home?PHPSESSID=".$session_id);
						}
					} else {
						header("Location: home?PHPSESSID=".$session_id);
					}
				}
				$error = "<p class='error'>{$user->error}</p>";
			}
		
			$url		= false;
			
			
			$redirect 	= "?redirect=home";
			if(isset($_SERVER['HTTP_REFERER']) ) {
				$redirect 	= "?redirect=".urlencode($_SERVER['HTTP_REFERER']);
			}
			if(isset($_GET['redirect'])) {
				$redirect 	= "?redirect=".urlencode($_GET['redirect']);
			}
			
			$vars = array(
							'error'		=> $error,
							'redirect'	=> $redirect,
							'user'		=> $_POST['username']
			);

			$content	= Content::get_static_content(Content::CONTENT_LOGIN, $vars);
			
			break;	
			
		case 'logout':
		
			$user->logout();
			
			// redirect to the last surfed page
			if(isset($_SERVER['HTTP_REFERER']) ) {
			
				$url = parse_url($_SERVER['HTTP_REFERER']);
				$tmp = explode("/", $url["path"]);
				$referer = $tmp[count($tmp)-1]."?";
				
				if($url["query"] != "") {
					$tmp = explode("&", $url["query"]);
					for($i=0; $i <count($tmp);$i++) {
						if(substr_count($tmp[$i], "PHPSESSID") == 0) {
							if($i > 0) {
								$referer .= "&";
							}
							$referer .= $tmp[$i];
						}
					} 
				}
				if($url["fragment"] != "") {
					$referer .= "#".$url["fragment"];
				}
				header("Location: ".$referer);
			} else {
				header("Location: home");
			}
			
			break;
				
		case 'register':
			$error = "";	
			
			if(isset($_POST["register"])) {
				if($user->register($_POST['email'], $_POST['password'], $_POST['title'], $_POST['name'], $_POST['surname'])) {
					
					$vars = array(
									'email'		=> $_POST['email']	
					);
					$content	= Content::get_static_content(Content::CONTENT_REGISTER_SUCCESS, $vars);

				} else {
					$error = "<p class='error'>{$user->error}</p><br />";
				}
			}
			
							
			$url		= false;
			
			if($content === false) {
				$vars = array(
								'error'		=> $error,
								'email'		=> $_POST['email'],
								'title'		=> $_POST['title'],
								'name'		=> $_POST['name'],
								'surname'	=> $_POST['surname']
				);

				$content	= Content::get_static_content(Content::CONTENT_REGISTER, $vars);
				
			} 				
							
			break;
			
		case 'activate-user-account':

			$tmp = explode("?", $_SERVER["REQUEST_URI"]);
			
			if( ($result = $user->activate_profile("?".$tmp[1])) === false) {
			
				$vars = array(
								'text'		=> $user->error
				);			
			
				$content	= Content::get_static_content(Content::CONTENT_ACTIVATE, $vars);
				$url		= false;
			} else {
			
				$vars = array(
								'text'		=> Content::CONTENT_ACTIVATE_SUCCESS
				);			
			
				$content	= Content::get_static_content(Content::CONTENT_ACTIVATE, $vars);
				$url		= false;			
			}
			
			break;
			
		case 'profile':
			$error					= "";
			$hidden_fields			= "";
			$hide_email_visible		= "";
			$hide_email_hide		= "";
			$password				= "";
			$title					= "";
			$fname					= "";
			$surname				= "";
			$department				= "";
			$organisation			= "";
			$address				= "";
			$country				= "";
			$website				= "";
	
			
			if(isset($_POST['update'])) {
			
				// save data								
				$post_data					= $_POST;
				$post_data["_action_save"]	= "Save";

				if($user->set_profile($post_data, array('c5_name_given' => 'Given Name', 'c5_name_family' => 'Family Name'), array('c5_url' => 'Homepage URL'), 'c4_password') === false) {
					
					$error = "<p class='error'>{$user->error}</p><br />";
						
				} else {
					
					$error = "<p class='success'>".Content::PROFILE_SUCC_UPDATE."</p><br />";
				}	
			
			} 
			
			
			// get user's data
			if( ($user_data = $user->get_profile()) === false) {
				$content = "<p class='error'>{$user->error}</p><br />";
			} else {
				foreach($user_data as $name => $attributes) {
				
					if(isset($_POST['update'])) {
						$attributes['value'] = $_POST[$name];
					}
				
					// hidden fields
					if($attributes["type"] == "hidden") {
						$hidden_fields .= "<input type='hidden' name='$name' id='{$attributes['id']}' value='{$attributes['value']}'>";
					} else if($attributes["type"] == "radio") {
						// radio (display or not email)
						$hide_email_visible = "<input type='radio' name='$name' value='FALSE'>";
						$hide_email_hide = "<input type='radio' name='$name' value='TRUE' checked='checked'>";
						
						if(array_key_exists("checked", $attributes)) {
							$hide_email_visible = "<input type='radio' name='$name' value='FALSE' checked='checked'>";
							$hide_email_hide = "<input type='radio' name='$name' value='TRUE'>";
						}
					} else {
					
					
						$attributes['value'] = htmlspecialchars($attributes['value'], ENT_QUOTES);
					
					
						// all other fields
						switch($name) {
						
							case "c4_password":
								$password = "<input type='password' name='$name' id='{$attributes['id']}' value='{$attributes['value']}'>";
								break;
							case "c5_name_honourific":
								$title = "<input type='input' name='$name' id='{$attributes['id']}' value='{$attributes['value']}' />";
								break;	
							case "c5_name_given":
								$fname = "<input type='input' name='$name' id='{$attributes['id']}' value='{$attributes['value']}' maxlength='{$attributes['maxlength']}'>";
								break;										
							case "c5_name_family":
								$surname = "<input type='input' name='$name' id='{$attributes['id']}' value='{$attributes['value']}' maxlength='{$attributes['maxlength']}'>";
								break;										
							case "c5_dept":
								$department = "<input type='input' name='$name' id='{$attributes['id']}' value='{$attributes['value']}' maxlength='{$attributes['maxlength']}'>";
								break;										
							case "c5_org":
								$organisation = "<input type='input' name='$name' id='{$attributes['id']}' value='{$attributes['value']}' maxlength='{$attributes['maxlength']}'>";
								break;										
							case "c5_country":
								$country = "<input type='input' name='$name' id='{$attributes['id']}' value='{$attributes['value']}' maxlength='{$attributes['maxlength']}'>";
								break;										
							case "c5_url":
								$website = "<input type='input' name='$name' id='{$attributes['id']}' value='{$attributes['value']}' maxlength='{$attributes['maxlength']}'>";
								break;											
							case "c5_address":
								$address = "<textarea name='$name' id='{$attributes['id']}'>{$attributes['value']}</textarea>";
								break;
																										
						}
					}
				}
			}
			

			$url		= false;			
			if($content === false) {
			
				$vars = array(
								"error"					=> $error,
								"session"				=> "?".htmlspecialchars(SID),
								"hiddenfields"			=> $hidden_fields,
								"hideemailvisible"		=> $hide_email_visible,
								"hideemailhide"			=> $hide_email_hide,
								"password"				=> $password,
								"title"					=> $title,
								"name"					=> $fname,
								"surname"				=> $surname,
								"department"			=> $department,
								"organisation"			=> $organisation,
								"address"				=> $address,
								"country"				=> $country,
								"url"					=> $website			
				);
				
				$content	= Content::get_static_content(Content::CONTENT_PROFILE, $vars);

			}
			
			break;	

					
	}
	
	// retrieve page content
	if($url !== false) {
		$content	= Content::get_dynamic_content($url);
	}
	$breadcrumb = Content::get_breadcrumb($page, $breadcrumb_items, $user->isLogged);
}

$main_menu	= Content::get_main_menu($page, $user->isLogged);

if($content === false) {
	//header("HTTP/1.0 404 Not Found");exit;
// ASK RORY!!!!
	
	// Error msg
	$content 	= Content::CONTENT_NOT_FOUND;
	$main_menu	= Content::get_main_menu("", $user->isLogged);
	$breadcrumb = "";
}
	

?>